
package com.hulk.ratel.web.control;

import com.google.common.base.Strings;
import com.google.common.eventbus.EventBus;
import com.hulk.common.util.JsonMapper;
import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.WebRteException;
import com.hulk.ratel.common.tool.ValidatorTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestAttribute;

/**
 * @author hulk
 * @version Create on: 2017年5月1日 下午7:05:05 Class description
 * @E-mail:29572320@qq.com
 */
@Slf4j
public abstract class AbsBaseCtl {

    protected static final JsonMapper json = JsonMapper.JSON_NON_DEFAULT_MAPPER;

    @Autowired
    protected EventBus eventBus;


    public abstract String excute(
            @RequestAttribute("partnerNo") String partnerNo,
            @RequestAttribute("traceId") String traceId,
            @RequestAttribute("plainText") String plainText,
            @RequestAttribute("partnerAESKey") String partnerAESKey,
            @RequestAttribute("workId") String workId,
            @RequestAttribute("intCutDate") String intCutDate);

    private <T> T jsonParse(String plainText, String workId, Class<T> clazz) {
        log.debug("AbsTxnSwitch.jsonParse");
        T jsonObj = json.fromJson(plainText, clazz);
        if (jsonObj == null) {
            throw new WebRteException(workId, BussErrCode.E_100007.getErrCode(), BussErrCode.E_100007.getErrDesc());
        }
        return jsonObj;
    }

    private String validateBean(Object o, String workId) {
        String validateResult = ValidatorTool.validateBean(o);
        if (!Strings.isNullOrEmpty(validateResult)) {
            throw new WebRteException(workId, BussErrCode.E_100001.getErrCode(), validateResult);
        }
        return validateResult;

    }

    public <T> T preHandle(String plainText, String workId, String validateResult, Class<T> clazz) {
        log.info("workId:{},plainText:{}", new Object[]{workId, plainText});
        T o = jsonParse(plainText, workId, clazz);
        return o;

    }


    public void checkValidateReq(String workId, Object reqBean) {
        String validateResult = validateBean(reqBean, workId);

    }

    public String postHandle(Object res, String partnerAESKey, String partnerNo, String workId) {
        //return contentUtil.postHandle(res, partnerAESKey, partnerNo, workId);

        return null;
    }


}
